let prev = Date.now()

const iRaf = (fn) => {
  const curr = Date.now()
  const ms = Math.max(0, 16 - (curr - prev))
  const id = setTimeout(fn, ms)
  prev = curr + ms
  return id
}
const iCancel = clearTimeout

export const raf = (fn) => {
  return iRaf(fn)
}

export const doubleRaf = (fn) => {
  raf(() => {
    raf(fn)
  })
}

export const cancelRaf = (id) => {
  iCancel(id)
}
